package com.wkh.project.customer.service.impl;

import com.wkh.project.common.result.Result;
import com.wkh.project.common.result.ResultCodeEnum;
import com.wkh.project.customer.client.CustomerInfoFeignClient;
import com.wkh.project.customer.service.CustomerService;
import com.wkh.project.model.entity.customer.CustomerInfo;
import com.wkh.project.model.form.customer.UpdateCustomerInfoForm;
import com.wkh.project.model.form.customer.UpdateWxPhoneForm;
import com.wkh.project.model.vo.customer.CustomerInfoVo;
import com.wkh.project.model.vo.customer.CustomerLoginVo;
import com.wkh.project.service.constant.RedisConstant;
import com.wkh.project.service.execption.GuiguException;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.StrUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    /**
     * 注入远程调用接口
     */
    @Resource
    private CustomerInfoFeignClient customerInfoFeignClient;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public String login(String code, HttpServletRequest request) {
        //1.进行远程调用
        Result<Long> loginResult = customerInfoFeignClient.login(code);
        //2.判断远程调用是否成功
        Integer resultCode = loginResult.getCode();
        if (!resultCode.equals(200)){
            throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
        }
        //3.获取远程调用成功后获得的用户id
        Long customerId = loginResult.getData();
        if (customerId == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //4.生成token字符串
        String token  = StrUtil.uuid().replaceAll("-","");
        //5.把token存储到redis中，并设置过期时间
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token,
                customerId.toString(),RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
        return token;
    }

    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        //1.根据token查询redis中对应的用户id
        String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        //2.判断这个id是否存在
        if (StrUtil.isBlank(customerId)){
            //todo 返回登录页面进行登录逻辑
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //3.远程调用获取登录信息
        Result<CustomerLoginVo> customerLoginVoResult = customerInfoFeignClient.getCustomerInfo(Long.parseLong(customerId));
        Integer code = customerLoginVoResult.getCode();
        //4.判断是否调用成功
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
        }
        //5.判断数据是否存在
        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        log.info("customerLoginVo值为：{}",customerLoginVo);
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //6.返回用户信息
        return customerLoginVo;
    }

    @Override
    public CustomerLoginVo getCustomerInfo(Long customerId) {
        //3.远程调用获取登录信息
        Result<CustomerLoginVo> customerLoginVoResult = customerInfoFeignClient.getCustomerInfo(customerId);
        Integer code = customerLoginVoResult.getCode();
        //4.判断是否调用成功
        if (code != 200){
            throw new GuiguException(ResultCodeEnum.FEIGN_FAIL);
        }
        //5.判断数据是否存在
        CustomerLoginVo customerLoginVo = customerLoginVoResult.getData();
        log.info("customerLoginVo值为：{}",customerLoginVo);
        if (customerLoginVo == null){
            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
        }
        //6.返回用户信息
        return customerLoginVo;
    }

    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
        return true;
    }

    @Override
    public Boolean updateCustomerInfo(UpdateCustomerInfoForm updateCustomerInfoForm) {
        customerInfoFeignClient.updateCustomerInfo(updateCustomerInfoForm);
        return true;
    }
}
